class ItemAlreadyExisting(KeyError): pass

class UnoverwritableDict(dict):
    """
    A dict-derived type, which forbidden overwriting existing entries.
    When trying to "d[k] = v" to d having an item for k, raise ItemAlreadyExisting.
    Also, "d.update(....)" is trying to update an existing item, raise ItemAlreadyExisting.
    If really need to modify item, delete item beforehand, like "del d[k]; d[k] = v".
    """
    def __init__(self):
        dict.__init__(self)
        
    def __setitem__(self, key, value):
        if key in self: 
            raise ItemAlreadyExisting("trying to overwrite item for a key: %s" % repr(key))
        dict.__setitem__(self, key, value)
    
    def update(self, items=None, **keywordArgs):
        if isinstance(items, dict):
            for item in items.iteritems():
                self.__setitem__(item)
        elif items is not None:
            for k, v in items:
                self.__setitem__(k, v)
        for item in keywordArgs.iteritems():
            self.__setitem__(item)
